package com.dc.bobaoge.project.v2.to.v3.business.core;

import com.alibaba.fastjson2.JSON;
import com.dc.bobaoge.base.mapper.message.MessagePraiseHistoryMapper;
import com.dc.bobaoge.base.mapper.message.MessagePraiseUserHistoryMapper;
import com.dc.bobaoge.common.constant.ConstantMq;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreMessageLaudMapper;
import com.dc.bobaoge.project.base.core.util.MessageCommonUtil;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import com.own.component.common.exception.BusinessSimpleException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

/**
 * MessageCollectionResolveMethod
 *
 * @author chenxueli
 * @date 2023-12-22 20:16:00
 */
@Slf4j
@Component
public class MessagePraiseResolveMethod implements ResolveMethod {

    private static final MessageProperties PROPERTIES = new MessageProperties();

    static {
        PROPERTIES.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
    }

    @Resource
    private OldCoreMessageLaudMapper oldCoreMessageLaudMapper;
    @Resource
    private MessagePraiseUserHistoryMapper messagePraiseUserHistoryMapper;
    @Resource
    private MessagePraiseHistoryMapper messagePraiseHistoryMapper;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource
    private MessageCommonUtil messageCommonUtil;

    /**
     * 修改的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        throw new BusinessSimpleException("暂无修改点赞信息同步的方法");
    }

    /**
     * 新增的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void add(SyncTrigger trigger) {
        // 查询信息
        var oldCoreMessageLaud = oldCoreMessageLaudMapper.selectById(trigger.getTargetId());
        if (oldCoreMessageLaud == null) {
            throw new RuntimeException("未查询到点赞的信息");
        }
        var map = new HashMap<String, Object>();
        map.put("userId", oldCoreMessageLaud.getLaudUserId());
        map.put("messageId", oldCoreMessageLaud.getLaudMsgId());
        map.put("type", "add");
        var content = JSON.toJSONString(map);
        rabbitTemplate.send(ConstantMq.MESSAGE_PRAISE_QUEUE, new Message(content.getBytes(StandardCharsets.UTF_8), PROPERTIES));
    }

    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "message_praise";
    }

}
